{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorFlow in R <a class=\"tocSkip\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# example from https://tensorflow.rstudio.com/tensorflow/articles/examples/mnist_softmax.html\n",
    "\n",
    "library(tensorflow)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define hyperparameters -----------\n",
    "\n",
    "batch_size <- 128\n",
    "num_classes <- 10\n",
    "steps <- 1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data Preparation -----------\n",
    "\n",
    "datasets <- tf$contrib$learn$datasets\n",
    "mnist <- datasets$mnist$read_data_sets(\"MNIST-data\", one_hot = TRUE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define Model ---------------\n",
    "\n",
    "# Create the model\n",
    "x <- tf$placeholder(tf$float32, shape(NULL, 784L))\n",
    "W <- tf$Variable(tf$zeros(shape(784L, num_classes)))\n",
    "b <- tf$Variable(tf$zeros(shape(num_classes)))\n",
    "\n",
    "y <- tf$nn$softmax(tf$matmul(x, W) + b)\n",
    "\n",
    "# Define loss and optimizer\n",
    "y_ <- tf$placeholder(tf$float32, shape(NULL, num_classes))\n",
    "cross_entropy <- tf$reduce_mean(-tf$reduce_sum(y_ * log(y), reduction_indices=1L))\n",
    "train_step <- tf$train$GradientDescentOptimizer(0.5)$minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Training the model --------\n",
    "\n",
    "# Create session and initialize  variables\n",
    "sess <- tf$Session()\n",
    "sess$run(tf$global_variables_initializer())\n",
    "\n",
    "# Train\n",
    "for (i in 1:steps) {\n",
    "  batches <- mnist$train$next_batch(batch_size)\n",
    "  batch_xs <- batches[[1]]\n",
    "  batch_ys <- batches[[2]]\n",
    "  sess$run(train_step,\n",
    "           feed_dict = dict(x = batch_xs, y_ = batch_ys))\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy: 0.9185 \n"
     ]
    }
   ],
   "source": [
    "# Evaluate the model --------\n",
    "\n",
    "correct_prediction <- tf$equal(tf$argmax(y, 1L), tf$argmax(y_, 1L))\n",
    "accuracy <- tf$reduce_mean(tf$cast(correct_prediction, tf$float32))\n",
    "score <-sess$run(accuracy,\n",
    "         feed_dict = dict(x = mnist$test$images, y_ = mnist$test$labels))\n",
    "\n",
    "cat('Test accuracy:', score, '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R 3.4.2",
   "language": "R",
   "name": "ir342"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.4.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
